define('admin/js/mod/system/Nav', [
    'utils/fetchEnhancer',
    'utils/helpers'], (fetch, helpers) => {

const NavConfig = {
    data: function() {
        return {
            editDialogVisible: false,
            formData: {},
            list: null
        }
    },
    methods: {
        fetchList: function() {
            let _self = this;

            fetch('/adminapi/listnav').then(function(json){
                if(0 === json.status) {
                    _self.list = json.data.data;
                }
            });
        },
        handleTableClick: function(e) {
            let _self = this;
            let t = e.target;
            let role = t.getAttribute('data-role');
            let id = t.getAttribute('data-id');

            if('edit' === role) {
                fetch('/adminapi/getnav?id=' + id).then(function(json){
                    if(0 === json.status) {
                        _self.formData = json.data.data,
                        _self.editDialogVisible = true;
                    }
                });
                return;
            }

            if('delete' === role) {
                this.$confirm('确实要删除吗？', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    fetch('/adminapi/deletenav', {
                        method: 'POST',
                        body: 'id=' + id
                    }).then((json) => {
                        if(0 === json.status) {
                            _self.$message.success('删除成功');
                            _self.fetchList();
                        }
                    });

                }).catch(() => {});
            }
        },
        handleAddBtnClick: function() {
            this.formData = {};
            this.editDialogVisible = true;
        },
        doAddOrEdit: function() {
            let _self = this;
            let ret = helpers.json2Query(this.formData);
            let url = this.formData.id ? '/adminapi/editnav' : '/adminapi/addnav';

            fetch(url, {
                method: 'POST',
                body: ret
            }).then(function(json){
                if(0 === json.status) {
                    _self.formData = {},
                    _self.editDialogVisible = false;
                    _self.fetchList();

                    _self.$message.success('操作成功');
                } else {
                    _self.$message.error('操作失败');
                }
            });
        }
    },
    mounted: function() {
        this.fetchList();
    },
    template:
`<div>
    <el-button type="primary" size="medium" @click="handleAddBtnClick">新增导航</el-button>
    <table class="x-table" @click="handleTableClick">
        <tr>
            <th>id</th><th>pid</th><th>标题</th><th>链接地址</th><th>链接类型</th><th>操作</th>
        </tr>
        <template v-if="list && list.length">
            <tbody v-for="item in list">
                <tr>
                    <td>{{ item.id }}</td>
                    <td>{{ item.pid }}</td>
                    <td>{{ item.title }}</td>
                    <td>{{ item.navurl }}</td>
                    <td>{{ item.linktype === '1' ? '本站链接' : '外站链接' }}</td>
                    <td>
                        <span class="x-table-cursor-text" data-role="edit" :data-id="item.id">修改</span>
                        <span class="x-table-cursor-text" data-role="delete" v-if="!item._child" :data-id="item.id">删除</span>
                    </td>
                </tr>
                <template v-if="item._child">
                    <tr v-for="sub in item._child">
                        <td>-</td>
                        <td>-</td>
                        <td>&nbsp;&nbsp;|__&nbsp;&nbsp;{{ sub.title }}</td>
                        <td>{{ sub.navurl }}</td>
                        <td>{{ sub.linktype === '1' ? '本站链接' : '外站链接' }}</td>
                        <td>
                            <span class="x-table-cursor-text" data-role="edit" :data-id="sub.id">修改</span>
                            <span class="x-table-cursor-text" data-role="delete" :data-id="sub.id">删除</span>
                        </td>
                    </tr>
                </template>
            </tbody>
        </template>
    </table>

    <el-dialog :title="formData.id ? '修改' : '新增'" :visible.sync="editDialogVisible">
        <el-form :model="formData">
            <el-form-item v-if="formData.id" label="id" label-width="100px">
                <el-input :disabled="true" v-model="formData.id"></el-input>
            </el-form-item>
            <el-form-item label="父级id" label-width="100px">
                <el-input v-model="formData.pid" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="标题" label-width="100px">
                <el-input v-model="formData.title" maxlength="20" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="链接" label-width="100px">
                <el-input v-model="formData.navurl" placeholder="http://" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="排序" label-width="100px">
                <el-input v-model="formData.listorder" maxlength="80" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="链接类型" label-width="100px">
                <el-select v-model="formData.linktype" placeholder="请选择">
                    <el-option key="local" label="本站链接" value="1"></el-option>
                    <el-option key="out" label="外站链接" value="2"></el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button @click="editDialogVisible=false">取消</el-button>
            <el-button type="primary" @click="doAddOrEdit">确定</el-button>
        </div>
    </el-dialog>
</div>`
};

return NavConfig;

});
